<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 配件ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */

quanli_require('module/base/goods/goodsFittings.class.php');

class ajax_goodsFittings {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品配件
                    $this->searchGoodsFittings();
                    break;
                case 2: //添加和修改商品配件
                    $this->saveGoodsFittings();
                    break;
                case 4: //删除商品配件
                    $this->deleteGoodsFittings();
                    break;
                case 5: //配件的使用状态
                    $this->statusGoodsFittings();
                    break;
                case 6: //待编辑信息查询
                    $this->searchOneGoodsFittings();
                    break;
                case 7: //最大序列号查询查询
                    $this->seachsort();
                    break;
                default:
                    backendError();
            }

        } else {
            backendError();
        }

    }
    /**
     * 查询商品配件
     */
    private function searchGoodsFittings() {

        global $G_PGLOB;

        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;

        $result_data  = $specHandler->goodsFittings( $_REQUEST ) ;

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

        if ($result_data) {

            $edit_permission = $G_PGLOB->has_permission(537);
            foreach($result_data as &$s_l) {

                $selectorUnit = '' ;
                if($s_l['fitUnit'] == 1)
                    $selectorUnit = '套 ' ;
                else if($s_l['fitUnit'] == 2)
                    $selectorUnit = '份 ' ;
                else if($s_l['fitUnit'] == 3)
                    $selectorUnit = '个 ' ;
                else if($s_l['fitUnit'] == 4)
                    $selectorUnit = '包 ' ;
                else
                    $selectorUnit = '支 ' ;

                if($edit_permission){
                    $result_array['rows'][] = array(
                        'id'   => $s_l['fitID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="goodsFittings.edit(' . $s_l['fitID'] . ')">维护值</a>&nbsp;' ,
                            $s_l['fitName'],
                            $selectorUnit,
                            $s_l['sortOrder'],
                            $s_l['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick="goodsFittings.doAction(' . $s_l['fitID'] . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="goodsFittings.doAction(' . $s_l['fitID'] . ', 1 ) " >禁用</a>'
                        )

                    );
                }else{
                    $result_array['rows'][] = array(
                        'id'   => $s_l['fitName'],
                        'cell' => array(
                            $s_l['fitName'] ,
                            $s_l['fitUnit'],
                            $s_l['sortOrder'],
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )
                    );
                }
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 配件保存和更新
     *
     */
    private function saveGoodsFittings () {
        global $G_PGLOB;
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;
        $result_array = $specHandler->saveGoodsFittings( $_REQUEST ) ;
        echo json_encode($result_array);
    }

    /**    
     * 删除配件
     *
     */
    private function deleteGoodsFittings () {
        global $G_PGLOB;

        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;
        $result_array = $specHandler->deleteFittings( $_REQUEST ) ;
        echo json_encode($result_array);
    }
    /**
     * 修改配件的使用状态
     *
     */
    private function statusGoodsFittings () {
        global $G_PGLOB;

        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;
        $result_array = $specHandler->changeUse( $_REQUEST ) ;
        echo json_encode($result_array);
    }

    /*
     * 待编辑信息
     *
     * */
    private  function searchOneGoodsFittings(){

        global $G_PGLOB;

        $arg_array = array('fitID' => $_REQUEST['fitID']);
        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;
        $result_data  = $specHandler->goodsSeachFittings( $arg_array ) ;
        echo json_encode($result_data);
    }

    //最大序列号查询
    private function seachsort(){

        global $G_PGLOB;

        $specHandler = new goodsFittings( $G_PGLOB->DBCA ) ;

        $sortOrder = $specHandler->maxSortOrder();

        echo json_encode(array('sortOrder' => $sortOrder));
    }

}